package com.loongxun.playwithyou.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.ui.PickerDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 预约
 * Created by Administrator on 2017/9/19.
 */
public class BespeakActivity extends Activity implements View.OnClickListener{

    private Context context;

    private TextView tv_top_title,tv_baspeak_data_time,tv_baspeak_hours_num
            ,tv_automatic_choice,tv_manual_choice;

    //预约时间,陪玩时长,
    private String playStartTime = "",hoursNum = "";

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private SimpleDateFormat sdfDD = new SimpleDateFormat("yyyy-MM-dd ");
    private SimpleDateFormat sdfHH = new SimpleDateFormat("HH:00");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_bespeak);
        context = BespeakActivity.this;

        initView();

        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR_OF_DAY, +1);
        String t1 = sdf.format(calendar.getTime());
        playStartTime = t1+":00";
        tv_baspeak_data_time.setText(playStartTime);
        hoursNum = "1";
        tv_baspeak_hours_num.setText(hoursNum + "小时");
    }

    private void initView() {
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("预约");
        findViewById(R.id.iv_back).setOnClickListener(this);

        tv_baspeak_data_time = (TextView) findViewById(R.id.tv_baspeak_data_time);
        tv_baspeak_data_time.setOnClickListener(this);
        tv_baspeak_hours_num = (TextView) findViewById(R.id.tv_baspeak_hours_num);
        tv_baspeak_hours_num.setOnClickListener(this);
        tv_automatic_choice = (TextView) findViewById(R.id.tv_automatic_choice);
        tv_automatic_choice.setOnClickListener(this);
        tv_manual_choice = (TextView) findViewById(R.id.tv_manual_choice);
        tv_manual_choice.setOnClickListener(this);

        findViewById(R.id.ll_choice).setVisibility(View.VISIBLE);
        findViewById(R.id.ll_coach).setVisibility(View.GONE);
    }



    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_baspeak_data_time:
                intent = new Intent(context, PickerDialog.class);
                intent.putExtra("type", 2);
                startActivityForResult(intent, 2);
                break;
            case R.id.tv_baspeak_hours_num:
                intent = new Intent(context, PickerDialog.class);
                intent.putExtra("type",3);
                startActivityForResult(intent, 3);
                break;
            //自动匹配
            case R.id.tv_automatic_choice:
                intent = new Intent(context, AutomaticActivity.class);
                intent.putExtra("playStartTime",playStartTime);
                intent.putExtra("hoursNum", hoursNum);
                startActivityForResult(intent, 5);
                break;
            case R.id.tv_manual_choice:
                intent = new Intent(context, MemberListActivity.class);
                intent.putExtra("playStartTime",playStartTime);
                intent.putExtra("hoursNum", hoursNum);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String num = data.getStringExtra("picker_left");
            if (requestCode == 2) {
                String t1;
                Calendar calendar;
                String picker_right = data.getStringExtra("picker_right");
                if (!"明天".equals(num)) {
                    t1 = sdfDD.format(new Date());
                    if ("".equals(picker_right)) {
                        calendar = Calendar.getInstance();
                        calendar.setTime(new Date());
                        calendar.add(Calendar.HOUR_OF_DAY, +1);
                        t1 = t1+sdfHH.format(calendar.getTime());
                    }else {
                        t1 = t1+picker_right;
                    }
                }else {
                    calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DAY_OF_MONTH, +1);
                    t1 = sdfDD.format(calendar.getTime());
                    if ("".equals(picker_right)) {
                        t1 = t1+"12:00";
                    }else {
                        t1 = t1+picker_right;
                    }
                }
                playStartTime = t1;
                tv_baspeak_data_time.setText(t1);
            }
            if (requestCode == 3) {
                if ("".equals(num)) {
                    hoursNum = "1";
                    tv_baspeak_hours_num.setText("1小时");
                }else {
                    hoursNum = num.substring(0,1);
                    tv_baspeak_hours_num.setText(num);
                }
            }
            if (requestCode == 5) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.HOUR_OF_DAY, +1);
                String t1 = sdf.format(calendar.getTime());
                playStartTime = t1+":00";
                tv_baspeak_data_time.setText(playStartTime);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
